package cz.semjob.framework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import cz.semjob.document.annotation.LabeledWords;
import cz.semjob.document.annotation.RelationWordsPair;
import cz.semjob.document.annotation.metadata.AnnotationTypeFactory;
import cz.semjob.document.processing.DocumentInstance;

@Component
public class ClassificatonRelationHelper {

	public Map<DocumentInstance, List<RelationWordsPair>> createCandidateRelationInstances(
			Map<DocumentInstance, DocumentAnnotationResults> foundAnnotations,
			String relationType) {
		Map<DocumentInstance, List<RelationWordsPair>> candidateRelations = new HashMap<DocumentInstance, List<RelationWordsPair>>();
		for (DocumentInstance documentInstance : foundAnnotations.keySet()) {

			List<RelationWordsPair> candidateDocumentRelations = new ArrayList<RelationWordsPair>();
			candidateRelations
					.put(documentInstance, candidateDocumentRelations);

			List<LabeledWords> foundDocumentAnnotation = foundAnnotations.get(
					documentInstance).getLabeledWords();
			for (LabeledWords annotation1 : foundDocumentAnnotation) {
				for (LabeledWords annotation2 : foundDocumentAnnotation) {
					if (!annotation1.equals(annotation2)) {
						if (annotation1
								.getWords()
								.get(0)
								.getSentence()
								.equals(annotation2.getWords().get(0)
										.getSentence())) {
							if (relationType
									.equals(AnnotationTypeFactory.taxonomicRelationType)) {
								if (annotation1.getLabel().equals(
										annotation2.getLabel())) {
									RelationWordsPair candidateRelation = new RelationWordsPair(
											annotation1, annotation2);
									candidateDocumentRelations
											.add(candidateRelation);
								}
							} else {
								RelationWordsPair candidateRelation = new RelationWordsPair(
										annotation1, annotation2);
								candidateDocumentRelations
										.add(candidateRelation);
							}

						}
					}
				}
			}

		}
		return candidateRelations;
	}
}
